package com.example.timmy.myapplication.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.timmy.myapplication.R;
import com.example.timmy.myapplication.utils.UiUtils;

/**
 * @Auther: Timmy
 * @Data: 2017/9/27
 * @Fuction: 渐变进度圆环
 */

public class ProgressRing extends View {

    private Context mContext;
    private int progressStartColor;
    private int progressEndColor;
    private int bgColor;
    private int progress;
    private float progressWidth;
    private int startAngle;
    private int sweepAngle;
    private boolean showAnim;

    private int mMeasureWidth;      // 宽度
    private int mMeasureHeight;     // 高度

    // 背景画笔
    private Paint bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
    // 进度画笔
    private Paint progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);

    private RectF pRectF;
    private float unitAngle;
    private int curProgress;
    private Paint mTextPaint;
    private int textColor;

    public ProgressRing(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initAttrs(attrs);
        initPaint();
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.ProgressRing);
        progressStartColor = ta.getColor(R.styleable.ProgressRing_pr_progress_start_color, Color.YELLOW);
        progressEndColor = ta.getColor(R.styleable.ProgressRing_pr_progress_end_color, progressStartColor);
        textColor = ta.getColor(R.styleable.ProgressRing_pr_text_color, Color.parseColor("#FF000000"));
        bgColor = ta.getColor(R.styleable.ProgressRing_pr_bg_color, Color.parseColor("#FFFFFF"));
        progress = ta.getInt(R.styleable.ProgressRing_pr_progress, 0);
        progressWidth = ta.getDimension(R.styleable.ProgressRing_pr_progress_width, 50f);
        // 起始开始画的角度
        startAngle = ta.getInt(R.styleable.ProgressRing_pr_start_angle, 270);
        // 画的角度终点
        sweepAngle = ta.getInt(R.styleable.ProgressRing_pr_sweep_angle, 360);
        showAnim = ta.getBoolean(R.styleable.ProgressRing_pr_show_anim, true);
        mText = ta.getString(R.styleable.ProgressRing_pr_text_str);
        ta.recycle();

        unitAngle = (float) (sweepAngle / 100.0);
    }

    private String mText = "水油";

    private void initPaint() {
        bgPaint.setStyle(Paint.Style.STROKE);
        bgPaint.setStrokeCap(Paint.Cap.ROUND);
        bgPaint.setStrokeWidth(progressWidth);

        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);
        progressPaint.setStrokeWidth(progressWidth);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(UiUtils.dp2px(18));
        mTextPaint.setColor(textColor);
        // paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mMeasureWidth = getMeasuredWidth();
        mMeasureHeight = getMeasuredHeight();
        if (pRectF == null) {
            float halfProgressWidth = progressWidth / 2;
            pRectF = new RectF(halfProgressWidth + getPaddingLeft(),
                    halfProgressWidth + getPaddingTop(),
                    mMeasureWidth - halfProgressWidth - getPaddingRight(),
                    mMeasureHeight - halfProgressWidth - getPaddingBottom());
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (!showAnim) {
            curProgress = progress;
        }

        drawBg(canvas);
        drawProgress(canvas);

        if (curProgress < progress) {
            curProgress++;
            postInvalidate();
        }

        drawText(canvas);
    }

    private void drawProgress(Canvas canvas) {

        for (int i = 0, currentProgress = (int) (curProgress * unitAngle); i <= currentProgress; i++) {

            float fraction = i / (float) currentProgress;
            progressPaint.setColor(getGradient(fraction, progressStartColor, progressEndColor));

            canvas.drawArc(pRectF,
                    startAngle + i,
                    1,
                    false,
                    progressPaint);
        }
    }


    private int bgStartColor = Color.parseColor("#26ff5d1a");
    private int bgEndColor = Color.parseColor("#26e9a137");


    private void drawBg(Canvas canvas) {
//         bgPaint.setColor(bgColor);

        for (int i = startAngle; i <= sweepAngle + startAngle; i++) {
            // bgPaint.setColor(getGradient(i / (float) st, bgStartColor, bgEndColor));

            float fraction = i / (float) (sweepAngle + startAngle);
            Log.i("背景颜色", "drawProgress: " + fraction);

            bgPaint.setColor(getGradient(fraction, bgStartColor, bgEndColor));

            canvas.drawArc(pRectF,
                    i,
                    1,
                    false,
                    bgPaint);
        }

    }

    private void drawText(Canvas canvas) {

        int textWidth = UiUtils.getTextWidth(mTextPaint, mText);
        int i = UiUtils.dp2px(20);
        canvas.drawText(mText, mMeasureWidth / 2 - textWidth - i, i, mTextPaint);
    }

    public void setText(String text) {
        mText = text;
        invalidate();
    }

    public void setProgress(int progress) {
        this.progress = progress;

        invalidate();
    }

    // 获取渐变色
    public int getGradient(float fraction, int startColor, int endColor) {
        if (fraction > 1) fraction = 1;
        int alphaStart = Color.alpha(startColor);
        int redStart = Color.red(startColor);
        int blueStart = Color.blue(startColor);
        int greenStart = Color.green(startColor);
        int alphaEnd = Color.alpha(endColor);
        int redEnd = Color.red(endColor);
        int blueEnd = Color.blue(endColor);
        int greenEnd = Color.green(endColor);
        int alphaDifference = alphaEnd - alphaStart;
        int redDifference = redEnd - redStart;
        int blueDifference = blueEnd - blueStart;
        int greenDifference = greenEnd - greenStart;
        int alphaCurrent = (int) (alphaStart + fraction * alphaDifference);
        int redCurrent = (int) (redStart + fraction * redDifference);
        int blueCurrent = (int) (blueStart + fraction * blueDifference);
        int greenCurrent = (int) (greenStart + fraction * greenDifference);
        return Color.argb(alphaCurrent, redCurrent, greenCurrent, blueCurrent);
    }
}
